package com.os.opencv.java.chapter5;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

public class Flip {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/Pictures/111.jpg");
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //绕y轴翻转
        Mat f1 = new Mat();
        Core.flip(src, f1, 1);

        //将两幅图像进行水平拼接
        List<Mat> mats = new ArrayList<>();
        mats.add(src);
        mats.add(f1);
        Mat d1 = new Mat();
        Core.hconcat(mats, d1);  //水平拼接

        //绕x轴翻转
        Mat f2 = new Mat();
        Core.flip(src, f2, 0);

        //绕x和y轴两个轴翻转
        Mat f3 = new Mat();
        Core.flip(src, f3, -1);

        //移除mats中的元素
        mats.remove(1);
        mats.remove(0);

        //将x轴翻转图像和两个轴翻转图像进行水平拼接
        mats.add(f2);
        mats.add(f3);
        Mat d2 = new Mat();
        Core.hconcat(mats, d2);

        //将两幅拼接后的图像垂直再拼接
        mats.remove(1);
        mats.remove(0);

        mats.add(d1);
        mats.add(d2);
        Mat d3 = new Mat();
        Core.vconcat(mats, d3);

        Mat d4 = new Mat();
        float scale = 0.4f;
        float width = d3.width();
        float height = d3.height();
        Imgproc.resize(d3, d4, new Size(width*scale, height*scale));

        //在屏幕上显示最后的结果
        HighGui.imshow("ALL", d4);
        HighGui.waitKey(0);

        System.exit(0);
    }
}
